package com.minglead.springboot.platform.sys.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.minglead.springboot.common.config.Global;
import com.minglead.springboot.common.persistence.DataEntity;
import com.minglead.springboot.common.utils.StringUtils;
import com.minglead.springboot.platform.sys.bean.CommonBean;

/**
 * 用户Entity
 */
public class User extends DataEntity<User> {

	private static final long serialVersionUID = 1L;
	private Office office;	// 归属部门
	private String loginName;// 登录名
	private String password;// 密码
	private String no;		// 工号
	private String name;	// 姓名
	private String email;	// 邮箱
	private String phone;	// 电话
	private String mobile;	// 手机
	private String loginIp;	// 最后登陆IP
	private Date loginDate;	// 最后登陆日期
	private Integer loginFlag;	// 是否允许登陆(1是；0否)
	private String photo;	// 头像
	private String extranetIp;//外网IP
	private Integer adminFlag;//是否超级管理员（只有超级管理员才能管理超级管理员）1--是，0不是
	private Integer sex;//1男；0女

	private String oldLoginName;// 原登录名
	private String newPassword;	// 新密码
	
	private Integer firstLogin;//是否是第一次登陆：1是(默认)；非1否；强制修改密码之后，这里可以修改为0 了。
	
	private Role role;	//当前登录用户角色
	private List<Role> roleList = new ArrayList<Role>(); // 拥有角色列表
	
	private String officeId;
	private String userOfficeId;//当前登录的用户的机构id
	
	private String userCertId; //证书唯一标识
	
	private Integer loginFailCounts;//当日登录失败次数
	private Date loginFailDate;//登录失败最新日期

	public String getOfficeId() {
		return officeId;
	}

	public void setOfficeId(String officeId) {
		this.officeId = officeId;
	}

	public User() {
		super();
		this.loginFlag = Integer.parseInt(Global.YES);
	}
	
	public User(String id){
		super(id);
	}

	public User(String id, String loginName){
		super(id);
		this.loginName = loginName;
	}

	public User(Role role){
		super();
		this.role = role;
	}
	
	
	public String getExtranetIp() {
		return extranetIp;
	}

	public void setExtranetIp(String extranetIp) {
		this.extranetIp = extranetIp;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}



	public Integer getLoginFlag() {
		return loginFlag;
	}

	public void setLoginFlag(Integer loginFlag) {
		this.loginFlag = loginFlag;
	}

	public String getId() {
		return id;
	}


	
	@NotNull(message="归属部门不能为空")
	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}

	@Length(min=1, max=100, message="登录名长度必须介于 1 和 100 之间")
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@JsonIgnore
	@Length(min=1, max=100, message="密码长度必须介于 1 和 100 之间")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Length(min=1, max=100, message="姓名长度必须介于 1 和 100 之间")
	public String getName() {
		return name;
	}
	
	@Length(min=1, max=100, message="工号长度必须介于 1 和 100 之间")
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Email(message="邮箱格式不正确")
	@Length(min=0, max=200, message="邮箱长度必须介于 1 和 200 之间")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	@Length(min=0, max=200, message="电话长度必须介于 1 和 200 之间")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Length(min=0, max=200, message="手机长度必须介于 1 和 200 之间")
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}


	public String getRemarks() {
		return remarks;
	}

	public Date getCreateDate() {
		return createDate;
	}


	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	public String getOldLoginName() {
		return oldLoginName;
	}

	public void setOldLoginName(String oldLoginName) {
		this.oldLoginName = oldLoginName;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}


	public String getUserCertId() {
		return userCertId;
	}

	public void setUserCertId(String userCertId) {
		this.userCertId = userCertId;
	}

	public Integer getLoginFailCounts() {
		return loginFailCounts;
	}

	public void setLoginFailCounts(Integer loginFailCounts) {
		this.loginFailCounts = loginFailCounts;
	}
	@JsonFormat(pattern = "yyyy-MM-dd")
	public Date getLoginFailDate() {
		return loginFailDate;
	}

	public void setLoginFailDate(Date loginFailDate) {
		this.loginFailDate = loginFailDate;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@JsonIgnore
	public List<Role> getRoleList() {
		return roleList;
	}
	
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	@JsonIgnore
	public List<String> getRoleIdList() {
		List<String> roleIdList = new ArrayList<String>();
		for (Role role : roleList) {
			roleIdList.add(role.getId());
		}
		return roleIdList;
	}

	public void setRoleIdList(List<String> roleIdList) {
		roleList = new ArrayList<Role>();
		for (String roleId : roleIdList) {
			Role role = new Role();
			role.setId(roleId);
			roleList.add(role);
		}
	}
	
	
	//通用是否下拉
	public static List<CommonBean> getYesOrNoList(){	
		
		List<CommonBean> list = new ArrayList<CommonBean>();		
		CommonBean c_1 = new CommonBean();
		c_1.setValue("1");
		c_1.setLabel(User.getYesOrNoLabel(Integer.parseInt(c_1.getValue())));	
	
		CommonBean c_0 = new CommonBean();
		c_0.setValue("0");
		c_0.setLabel(User.getYesOrNoLabel(Integer.parseInt(c_0.getValue())));	
			
		list.add(c_1);
		list.add(c_0);

		return list;
	}


	public static String getYesOrNoLabel(Integer type){
		if(type==null)
			return "null";
		if(1==type){
			return "是";
		}else if(0==type){
			return "否";
		}else{
			return "未知状态";
		}
	}
	
	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	public String getRoleNames() {
		return roleList.toString();//有待完善
	}
	
	public boolean isAdmin(){
		if(this.getAdminFlag()!=null&&1==this.getAdminFlag())
			return true;
		return false;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + "]";
	}


	public String getUserOfficeId() {
		return userOfficeId;
	}

	public void setUserOfficeId(String userOfficeId) {
		this.userOfficeId = userOfficeId;
	}

	public Integer getFirstLogin() {
		return firstLogin;
	}

	public void setFirstLogin(Integer firstLogin) {
		this.firstLogin = firstLogin;
	}


	public Integer getAdminFlag() {
		return adminFlag;
	}

	public void setAdminFlag(Integer adminFlag) {
		this.adminFlag = adminFlag;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}
	
	
}